package com.crm.springtask;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.crm.entity.Clue;
import com.crm.mapper.CluesMapper;
import com.crm.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Component
public class TestSpringTask {
    @Autowired
    private CluesMapper cluesMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    public static final String clueKey = "CLUE:";
    public static final Integer CLUE_STATUS_OVERTIME = 3;
    public static final Integer CLUE_POOL_CODE = 3;

    @Scheduled(cron = "0 0 0 * * ? ")//指定每天0.0.0就触发一次
    public void test(){
        //查询所有线索池（transfer为3）
        List<Clue> clueList = cluesMapper.selectList(Wrappers.<Clue>lambdaQuery().eq(Clue::getTransfer,CLUE_POOL_CODE));
        for (Clue clue : clueList) {
            String clueFlag = (String) redisTemplate.opsForValue().get(clueKey + clue.getId());
            if (StringUtils.isEmpty(clueFlag)){
                clue.setStatus(CLUE_STATUS_OVERTIME);
                cluesMapper.update(clue, Wrappers.<Clue>lambdaQuery().eq(Clue::getId, clue.getId()));
            }
        }



    }
}